﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class FShoppingCart : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bindata();
        }
    }
    public void Bindata()
    {
        ShoppingCartBOL sBOL = new ShoppingCartBOL();
        GridView1.DataSource = sBOL.LoadShoppingcart();
        GridView1.DataBind();
    }
    protected void UpdateRecord(object so, GridViewUpdateEventArgs e)
    {
        GridViewRow row = GridView1.Rows[e.RowIndex];
        TextBox tShoppingID = (TextBox)row.FindControl("txtShoppingID");
        TextBox tProID = (TextBox)row.FindControl("txtProID");
        TextBox tIdentification = (TextBox)row.FindControl("txtIdentification");
        TextBox tQuantity = (TextBox)row.FindControl("txtQuantity");
        TextBox tDateShopping = (TextBox)row.FindControl("txtDateShopping");
        TextBox tTotalPrice = (TextBox)row.FindControl("txtTotalPrice");
        TextBox tstatus = (TextBox)row.FindControl("txtstatus");

        ShoppingCart s = new ShoppingCart();
        ShoppingCartBOL sBOL = new ShoppingCartBOL();

        try
        {
            s.ShoppingID = tShoppingID.Text;
            s.ProID = tProID.Text;
            s.Identification = tIdentification.Text;
            s.Quantity = Convert.ToInt32(tQuantity.Text);
            s.DateShopping = Convert.ToDateTime(tDateShopping.Text);
            s.TotalPrice = Convert.ToDouble(tTotalPrice.Text);
            s.Status = tstatus.Text;

            int result = sBOL.updateShopingcart(s);
            if (result > 0)
            {
                LResult.Text = "Update record is successful!";
            }
            else
                LResult.Text = "Update record is error";
        }
        catch (Exception ex)
        {
            LResult.Text = ex.Message;
        }
        finally
        {
            s = null;
            sBOL = null;
        }
        GridView1.EditIndex = -1;
        Bindata();
    }
    protected void EditRecord(object s, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        Bindata();
    }
    protected void CancelRecord(object s, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        Bindata();
    }
    protected void DeleteRecord(object s, GridViewDeleteEventArgs e)
    {
        string tShoppingID = GridView1.DataKeys[e.RowIndex].Value.ToString();

        ShoppingCart sh = new ShoppingCart();
        ShoppingCartBOL sBOL = new ShoppingCartBOL();
        try
        {

            sh.ShoppingID = tShoppingID;
            int result = sBOL.deleteShoppingcart(sh);
            if (result > 0)
                LResult.Text = "Delete record is successful.";
            else
                LResult.Text = "Delete record is error.";
        }
        catch (Exception ex)
        {
            LResult.Text = ex.Message;
        }
        finally
        {

            sh = null;
            sBOL = null;
        }
        GridView1.EditIndex = -1;
        Bindata();
    }
}